/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import static com.hvtt.web.controller.ReportEcxelController.mergeColspan;
import static com.hvtt.web.controller.ReportEcxelController.mergeColspanNoBoder;
import static com.hvtt.web.controller.ReportEcxelController.mergeRowspan;
import com.hvtt.web.models.RPT_ThongKeTaiKhoanModel;
import com.hvtt.web.models.datatable.DefaultLazyDataModel;
import com.hvtt.web.utilities.Utilities;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ComponentSystemEvent;
import javax.servlet.ServletContext;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.primefaces.component.column.Column;
import org.primefaces.component.panelgrid.PanelGrid;
import org.primefaces.component.row.Row;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.TreeNode;
import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.LineChartSeries;
import org.primefaces.model.chart.PieChartModel;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class RPT_ThongKeTaiKhoanController implements Serializable {
      private WEBConnection DAL = WEBConnection.getInstance();
      ReportEcxelController controller=new ReportEcxelController();
    private StreamedContent fileExporter;
       private String cpid = "";
    private String hdh = "";
    private String gameID = "";
    private String congTT = "";
    private String telco = "";
    private String loaiThongKe = "";
    private Date tuNgay = Calendar.getInstance().getTime();
    private Date denNgay = new java.sql.Date(0);
    private long giaTriTheNap = 0;
    private String page = "";
    private String loaiBD = "";
    
    private long tong1=0;
    private long tong2=0;
    private long tong3=0;
    private long tong4=0;
            

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    /**
     * @return the cpid
     */
    public String getCpid() {
        return cpid;
    }

    /**
     * @param cpid the cpid to set
     */
    public void setCpid(String cpid) {
        this.cpid = cpid;
    }

    /**
     * @return the hdh
     */
    public String getHdh() {
        return hdh;
    }

    /**
     * @param hdh the hdh to set
     */
    public void setHdh(String hdh) {
        this.hdh = hdh;
    }

    /**
     * @return the gameID
     */
    public String getGameID() {
        return gameID;
    }

    /**
     * @param gameID the gameID to set
     */
    public void setGameID(String gameID) {
        this.gameID = gameID;
    }

    /**
     * @return the congTT
     */
    public String getCongTT() {
        return congTT;
    }

    /**
     * @param congTT the congTT to set
     */
    public void setCongTT(String congTT) {
        this.congTT = congTT;
    }

    /**
     * @return the telco
     */
    public String getTelco() {
        return telco;
    }

    /**
     * @param telco the telco to set
     */
    public void setTelco(String telco) {
        this.telco = telco;
    }

    /**
     * @return the loaiThongKe
     */
    public String getLoaiThongKe() {
        return loaiThongKe;
    }

    /**
     * @param loaiThongKe the loaiThongKe to set
     */
    public void setLoaiThongKe(String loaiThongKe) {
        this.loaiThongKe = loaiThongKe;
    }

    /**
     * @return the tuNgay
     */
    public Date getTuNgay() {
        return tuNgay;
    }

    /**
     * @param tuNgay the tuNgay to set
     */
    public void setTuNgay(Date tuNgay) {
        this.tuNgay = tuNgay;
    }

    /**
     * @return the denNgay
     */
    public Date getDenNgay() {
        return denNgay;
    }

    /**
     * @param denNgay the denNgay to set
     */
    public void setDenNgay(Date denNgay) {
        this.denNgay = denNgay;
    }

    /**
     * @return the giaTriTheNap
     */
    public long getGiaTriTheNap() {
        return giaTriTheNap;
    }

    /**
     * @param giaTriTheNap the giaTriTheNap to set
     */
    public void setGiaTriTheNap(long giaTriTheNap) {
        this.giaTriTheNap = giaTriTheNap;
    }

    /**
     * @return the loaiBD
     */
    public String getLoaiBD() {
        return loaiBD;
    }

    /**
     * @param loaiBD the loaiBD to set
     */
    public void setLoaiBD(String loaiBD) {
        this.loaiBD = loaiBD;
    }

    /**
     * @return the tong1
     */
    public long getTong1() {
        return tong1;
    }

    /**
     * @param tong1 the tong1 to set
     */
    public void setTong1(long tong1) {
        this.tong1 = tong1;
    }

    /**
     * @return the tong2
     */
    public long getTong2() {
        return tong2;
    }

    /**
     * @param tong2 the tong2 to set
     */
    public void setTong2(long tong2) {
        this.tong2 = tong2;
    }

    /**
     * @return the tong3
     */
    public long getTong3() {
        return tong3;
    }

    /**
     * @param tong3 the tong3 to set
     */
    public void setTong3(long tong3) {
        this.tong3 = tong3;
    }

    /**
     * @return the tong4
     */
    public long getTong4() {
        return tong4;
    }

    /**
     * @param tong4 the tong4 to set
     */
    public void setTong4(long tong4) {
        this.tong4 = tong4;
    }
    
    
     private LazyDataModel<RPT_ThongKeTaiKhoanModel> thongKeTaiKhoanModelLazy;

    public void initialize() {
        thongKeTaiKhoanModelLazy = new DefaultLazyDataModel(this.RPT_ThongKeTaiKhoan(), new Field[]{});

    }

    public void bindingDataThongKeTaiKhoan(ComponentSystemEvent event) {
        if (!FacesContext.getCurrentInstance().isPostback()) {
            builtParam();
            builtRptThongKeTK();
        }
    }

    private void builtParam() {
        this.setCpid("");
        this.setGameID("");
        this.setHdh("");
        Calendar tngay = Calendar.getInstance();
        tngay.set(2013, 1, 1);
        this.setTuNgay(tngay.getTime());
        this.setDenNgay(Calendar.getInstance().getTime());
    }

 
    
       private void builtRptThongKeTK() {
        List<RPT_ThongKeTaiKhoanModel> lst = this.RPT_ThongKeTaiKhoan();
        PanelGrid dataTable = (PanelGrid) FacesContext.getCurrentInstance().getViewRoot().findComponent("frmBCThongKeTK:table");
        Row r;
        int rowSpan = 0;
        String cp = "";
        HtmlOutputText outputText;
        Column col;
        double tong = 0;
        for (int i = 0; i < lst.size(); i++) {
            RPT_ThongKeTaiKhoanModel item = lst.get(i);
            r = new Row();
            if (!cp.equalsIgnoreCase(item.getCpid())) {
                if (!cp.isEmpty()) {
                    Row rr = (Row) dataTable.getChildren().get(dataTable.getChildren().size() - rowSpan);
                    col = new Column();
                    outputText = new HtmlOutputText();
                    outputText.setValue(cp);
                    col.setRowspan(rowSpan);
                    col.getChildren().add(outputText);
                    rr.getChildren().add(0, col);
                    col = new Column();
                    outputText = new HtmlOutputText();
                    outputText.setValue(Utilities.formatNumber("###,###.##", tong));
                    col.setRowspan(rowSpan);
                    col.getChildren().add(outputText);
                    rr.getChildren().add(col);
                }
                rowSpan = 0;
                cp = item.getCpid();
                tong = 0;

                col = new Column();
                outputText = new HtmlOutputText();
                outputText.setValue(item.getHdh());
                col.getChildren().add(outputText);
                r.getChildren().add(col);

                col = new Column();
                outputText = new HtmlOutputText();
                outputText.setValue(item.getSoTK());
                col.getChildren().add(outputText);
                r.getChildren().add(col);

                tong += item.getSoTK();
                rowSpan++;
            } else {
                col = new Column();
                outputText = new HtmlOutputText();
                outputText.setValue(item.getHdh());
                col.getChildren().add(outputText);
                r.getChildren().add(col);

                col = new Column();
                outputText = new HtmlOutputText();
                outputText.setValue(item.getSoTK());
                col.getChildren().add(outputText);
                r.getChildren().add(col);
                rowSpan++;
                tong += item.getSoTK();
            }
            dataTable.getChildren().add(r);
            if (i == lst.size() - 1) {
                Row rr = (Row) dataTable.getChildren().get(dataTable.getChildren().size() - rowSpan);
                col = new Column();
                outputText = new HtmlOutputText();
                outputText.setValue(cp);
                col.setRowspan(rowSpan);
                col.getChildren().add(outputText);
                rr.getChildren().add(0, col);
                col = new Column();
                outputText = new HtmlOutputText();
                outputText.setValue(Utilities.formatNumber("###,###.##", tong));
                col.setRowspan(rowSpan);
                col.getChildren().add(outputText);
                rr.getChildren().add(col);
            }
        }
    }

      public List<RPT_ThongKeTaiKhoanModel> RPT_ThongKeTaiKhoan() {
        List<RPT_ThongKeTaiKhoanModel> lst = null;
        try {
            WEBParamCollection Params = new WEBParamCollection();
            Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
            Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
            Params.add(("-1".equals(this.getHdh()) ? "" : this.getHdh()));
            Params.add(this.getTuNgay());
            Params.add(this.getDenNgay());
            lst = DAL.executeQuery("{call RPT_ThongKeTaiKhoan(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_ThongKeTaiKhoanModel.class);
            return lst;
        } catch (SQLException ex) {
            Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return lst == null ? new ArrayList() : lst;
    }

       public PieChartModel RPT_ThongKeTaiKhoan_Chart_TyLeUserTheoHDH_CP0() throws SQLException {
        PieChartModel c = new PieChartModel();
        List<RPT_ThongKeTaiKhoanModel> lst;

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getHdh()) ? "" : this.getHdh()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_ThongKeTaiKhoan_Chart_TyLeUserTheoHeDieuHanhCuaCP_0(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_ThongKeTaiKhoanModel.class);

        for (RPT_ThongKeTaiKhoanModel m : lst) {
            c.set(m.getHdh() + " " + String.valueOf(m.getSoTK()), m.getSoTK());
        }
        return c;
    }
    public PieChartModel RPT_ThongKeTaiKhoan_Chart() throws SQLException {
        PieChartModel c = new PieChartModel();
        List<RPT_ThongKeTaiKhoanModel> lst;

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getHdh()) ? "" : this.getHdh()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_ThongKeTaiKhoan_Chart(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_ThongKeTaiKhoanModel.class);

        for (RPT_ThongKeTaiKhoanModel m : lst) {
            c.set(m.getHdh() + " " + String.valueOf(m.getSoTK()), m.getSoTK());
        }
        return c;
    }
     public PieChartModel RPT_ThongKeTaiKhoan_Chart_TyLeUserTheoCP() throws SQLException {
        PieChartModel c = new PieChartModel();
        List<RPT_ThongKeTaiKhoanModel> lst;

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getHdh()) ? "" : this.getHdh()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_ThongKeTaiKhoan_Chart_TyLeUserTheoCP(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_ThongKeTaiKhoanModel.class);

        for (RPT_ThongKeTaiKhoanModel m : lst) {
            c.set(m.getCpName()+ " " + String.valueOf(m.getSoTK()), m.getSoTK());
        }
        return c;
    }
    
      public CartesianChartModel  RPT_ThongKeTaiKhoan_Chart_Line_Ngay() throws SQLException {
    
        List<RPT_ThongKeTaiKhoanModel> lst;

        
        
       CartesianChartModel model=new CartesianChartModel();

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getHdh()) ? "" : this.getHdh()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_ThongKeTaiKhoan_Chart_Line_Ngay(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_ThongKeTaiKhoanModel.class);
        
        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
            
            List<RPT_ThongKeTaiKhoanModel> lst1=DAL.executeQuery("Select distinct CPID,CPName from users_report where users_report.CPID<>''",RPT_ThongKeTaiKhoanModel.class);
            for(RPT_ThongKeTaiKhoanModel m:lst1)
            {
                boolean bln=false;
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Số user theo " + m.getCpName());
                int i=0;
                while(i<lst.size())
                {
                    if(!l.getData().containsKey(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM")))
                    {
                        l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getCpid() == null ? m.getCpid() == null : lst.get(i).getCpid().equals(m.getCpid()))
                    {
                         l.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), lst.get(i).getSoTK());
                         l.setShowLine(true);
                         bln=true;
                    }
                    i++;
                }
                 if(bln)
                    model.addSeries(l);
            }
            
            LineChartSeries l1=new LineChartSeries();
            l1.setLabel("Tổng số user");
            int i=0;
            while(i<lst.size())
            {

                  l1.set(Utilities.parseDateTime(lst.get(i).getNgay(), "dd/MM"), lst.get(i).getTongCong());
                 l1.setShowLine(true);
                     i++;
             
                  
            }
            
            
             model.addSeries(l1);
        }
        else
        {
              LineChartSeries l=new LineChartSeries();
            l.setLabel("Số user theo CP");
           
            for (RPT_ThongKeTaiKhoanModel m : lst) {

                l.set(Utilities.parseDateTime(m.getNgay(), "dd/MM"), m.getSoTK());
                 l.setShowLine(true);



               
            }     
             model.addSeries(l);
            
        }
      
       
        return model;
    }
      public CartesianChartModel  RPT_ThongKeTaiKhoan_Chart_Line_thang() throws SQLException {
    
        List<RPT_ThongKeTaiKhoanModel> lst;

        
        
       CartesianChartModel model=new CartesianChartModel();

        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getHdh()) ? "" : this.getHdh()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        lst = DAL.executeQuery("{call RPT_ThongKeTaiKhoan_Chart_Line_thang(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_ThongKeTaiKhoanModel.class);
        
        if(Sys_NguoiDungController.findUser().isIsAdmin())
        {
             List<RPT_ThongKeTaiKhoanModel> lst1=DAL.executeQuery("Select distinct CPID,CPName from users_report where users_report.CPID<>''",RPT_ThongKeTaiKhoanModel.class);
            for(RPT_ThongKeTaiKhoanModel m:lst1)
            {
                LineChartSeries l=new LineChartSeries();
                l.setLabel("Số user theo " + m.getCpName());
                int i=0;
                boolean bln=false;
                while(i<lst.size())
                {
                    if(!l.getData().containsKey(lst.get(i).getThang()))
                    {
                        l.set(lst.get(i).getThang(), 0);
                        l.setShowLine(true);
                    }
                    if(lst.get(i).getCpid() == null ? m.getCpid() == null : lst.get(i).getCpid().equals(m.getCpid()))
                    {
                         l.set(lst.get(i).getThang(), lst.get(i).getSoTK());
                         l.setShowLine(true);
                         bln=true;
                    }
                    i++;
                }
                 if(bln)
                    model.addSeries(l);
            }
            
            LineChartSeries l1=new LineChartSeries();
              l1.setLabel("Tổng số user");
            int i=0;
            while(i<lst.size())
            {

                  l1.set(lst.get(i).getThang(), lst.get(i).getTongCong());
                 l1.setShowLine(true);
                     i++;
             
                  
            }
            
            
             model.addSeries(l1);
        }
        else
        {
            LineChartSeries l=new LineChartSeries();
            l.setLabel("Số user theo CP");
           
            for (RPT_ThongKeTaiKhoanModel m : lst) {

                l.set(m.getThang(), m.getSoTK());
                 l.setShowLine(true);

            }     
            model.addSeries(l);
        }
       
        return model;
    }

    
    public void cmd_RPT_ThongKeTaiKhoan_Excel_Click(ActionEvent event) throws SQLException, ParseException {
        Map mapParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.setCpid(String.valueOf(mapParams.get("CPID")));
        this.setGameID(String.valueOf(mapParams.get("Game")));
        this.setHdh(String.valueOf(mapParams.get("HDH")));
        String strTuNgay = String.valueOf(mapParams.get("TuNgay"));
        String strDenNgay = String.valueOf(mapParams.get("DenNgay"));
        setTuNgay(Utilities.parseDate(strTuNgay, "dd/MM/yyyy"));
        setDenNgay(Utilities.parseDate(strDenNgay, "dd/MM/yyyy"));

        List<RPT_ThongKeTaiKhoanModel> lst = RPT_ThongKeTaiKhoan();

        setFileExporter(controller.thongKeTaiKhoanToExcel1(tuNgay, denNgay, lst));
        if (getFileExporter() != null) {
            try {
                Utilities.deleteFile(new File(controller.getLocalPath() + "/" + getFileExporter().getName()));
            } catch (IOException ex) {
                Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void cmd_RPT_ThongKeTaiKhoan_ThongKe_Click(ActionEvent event) throws SQLException, ParseException {
        Map mapParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.setCpid(String.valueOf(mapParams.get("CPID")));
        this.setGameID(String.valueOf(mapParams.get("Game")));
        this.setHdh(String.valueOf(mapParams.get("HDH")));
        String strTuNgay = String.valueOf(mapParams.get("TuNgay"));
        String strDenNgay = String.valueOf(mapParams.get("DenNgay"));
        setTuNgay(Utilities.parseDate(strTuNgay, "dd/MM/yyyy"));
        setDenNgay(Utilities.parseDate(strDenNgay, "dd/MM/yyyy"));

      this.setPage("/layout/Web/thongketaikhoan.xhtml");
      
      

    }

    public void cmd_RPT_ThongKeTaiKhoan_Chart_Click(ActionEvent event) throws SQLException, ParseException {
        Map mapParams = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        this.setCpid(String.valueOf(mapParams.get("CPID")));
        this.setGameID(String.valueOf(mapParams.get("Game")));
        this.setHdh(String.valueOf(mapParams.get("HDH")));
        this.setLoaiBD(String.valueOf(mapParams.get("Loai")));
        String strTuNgay = String.valueOf(mapParams.get("TuNgay"));
        String strDenNgay = String.valueOf(mapParams.get("DenNgay"));
        setTuNgay(Utilities.parseDate(strTuNgay, "dd/MM/yyyy"));
        setDenNgay(Utilities.parseDate(strDenNgay, "dd/MM/yyyy"));
        switch (this.getLoaiBD()) {
            case "2":
                this.setPage("/layout/Chart/thongketaikhoan.xhtml");
                break;
            case "4":
                this.setPage("/layout/Chart/thongketaikhoan_line_ngay.xhtml");
                break;
            case "5":
                this.setPage("/layout/Chart/thongketaikhoan_line_thang.xhtml");
                break;
        }
            
    }

  
    public TreeNode RPT_ThongKeTaiKhoan_ThongKe() throws SQLException {
        TreeNode root = new DefaultTreeNode(new RPT_ThongKeTaiKhoanModel(), null);
        WEBParamCollection Params = new WEBParamCollection();
        Params.add(("-1".equals(this.getCpid()) ? "" : this.getCpid()));
        Params.add(("-1".equals(this.getGameID()) ? "" : this.getGameID()));
        Params.add(("-1".equals(this.getHdh()) ? "" : this.getHdh()));
        Params.add(this.getTuNgay());
        Params.add(this.getDenNgay());
        List<RPT_ThongKeTaiKhoanModel> lst = DAL.executeQuery("{call RPT_ThongKeTaiKhoan(?,?,?,?,?)}", Command.sqlStoredProcedure, Params, RPT_ThongKeTaiKhoanModel.class);
        int i = 0;
        String strCPID;
        long lTong1=0;
        while (i < lst.size()) {
            RPT_ThongKeTaiKhoanModel m = new RPT_ThongKeTaiKhoanModel();
            m.setCpName(lst.get(i).getCpName());
            TreeNode noteParent = new DefaultTreeNode(m, root);
          


            strCPID = lst.get(i).getCpid();

            int iCount = 0;
            if (i < lst.size()) {
                while (i < lst.size() && (lst.get(i).getCpid() == null ? strCPID == null : lst.get(i).getCpid().equals(strCPID))) {
                       String strHDH = lst.get(i).getHdh();
                        RPT_ThongKeTaiKhoanModel m_Teleco = new RPT_ThongKeTaiKhoanModel();
                        //m_Teleco.setCongTT("");
                        m_Teleco.setCpName(lst.get(i).getHdh());
                       // m_Teleco.setTelco("");

                        TreeNode noteParent_Teleco = new DefaultTreeNode(m_Teleco, noteParent);
                        noteParent_Teleco.setExpanded(true);
                        
                        int iTongHDH=0;
                     while (i < lst.size() && (lst.get(i).getHdh()== null ? strHDH== null : lst.get(i).getHdh().equals(strHDH))
                             && (lst.get(i).getCpid() == null ? strCPID == null : lst.get(i).getCpid().equals(strCPID))
                             ) {
                           
                            //lst.get(i).setHdh("");
                            RPT_ThongKeTaiKhoanModel m1=lst.get(i);
                            m1.setCpName("");
                            if(m1.getUsername().length()>=4)
                            {
                                m1.setUsername(m1.getUsername().substring(0, 4)+"****");
                            }
                            
                            
                            iTongHDH += m1.getSoTK();
                            
                            lTong1+=m1.getSoTK();
                            
                            noteParent_Teleco.getChildren().add(new DefaultTreeNode(m1));
                            
                            i++;
                     }
                     //nodeHDH.setExpanded(true);
                     m_Teleco.setSoTK(iTongHDH);
                    // m_Teleco.setCpName(m_Teleco.getHdh());
                    // noteParent.getChildren().add(nodeHDH);
                     iCount+=iTongHDH;                   
                }
                m.setSoTK(iCount);
            }
            noteParent.setExpanded(true);
            // i++;
        }
        this.setTong1(lTong1);
        this.setTong2(0);
        this.setTong3(0);
        return root;
    }

    /**
     * @return the fileExporter
     */
    public StreamedContent getFileExporter() {
        return fileExporter;
    }

    /**
     * @param fileExporter the fileExporter to set
     */
    public void setFileExporter(StreamedContent fileExporter) {
        this.fileExporter = fileExporter;
    }

    
    
}
